package com.music.service.music.impl;

import com.music.dto.collection.CollectionPlaylistDTO;
import com.music.dto.music.PlaylistDTO;
import com.music.dto.tag.TagDTO;
import com.music.dto.utils.PagingDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.entity.collection.CollectionPlaylistEntity;
import com.music.mapper.music.CollectionPlaylistMapper;
import com.music.service.music.CollectionPlaylistService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CollectionPlaylistServiceImpl implements CollectionPlaylistService {

    @Resource
    private CollectionPlaylistMapper collectionPlaylistMapper;
    private final Logger logger = LoggerFactory.getLogger(PlaylistServiceImpl.class);

    @Override
    public TableRspDTO list4Table(Long userId, TableReqDTO tableReqDTO) throws Exception {
        Long count = collectionPlaylistMapper.count4Table(userId,
                tableReqDTO.parseQueryParam("playlistName"));
        List<PlaylistDTO> listPlaylist = collectionPlaylistMapper.list4Table(
                tableReqDTO.parseQueryParam("playlistName"),
                tableReqDTO.getStart(),
                tableReqDTO.getPageSize(),
                userId);
        List<TagDTO> listAllTag = new ArrayList<>(collectionPlaylistMapper.listAllTag());
        Map<Long, String> tagMap = new HashMap<>();
        for (TagDTO tagDTO : listAllTag) {
            tagMap.put(tagDTO.getTagId(), tagDTO.getTagName());
        }
        for (PlaylistDTO playlistDTO : listPlaylist) {
            if (playlistDTO.getTagId() == null)
                continue;
            String[] tagIds = playlistDTO.getTagId().split("/");
            List<String> tagNames = new ArrayList<>();
            for (String tagId : tagIds) {
                tagNames.add(tagMap.get(Long.parseLong(tagId)));
            }
            playlistDTO.setTagName(StringUtil.join(tagNames.toArray(), "/"));
        }
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                listPlaylist);
        return new TableRspDTO(pagingDTO);
    }
    
    @Override
    public Integer add(Long userId, CollectionPlaylistDTO collectionPlaylistDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        CollectionPlaylistEntity collectionPlaylistEntity = mapperFactory.getMapperFacade()
                .map(collectionPlaylistDTO, CollectionPlaylistEntity.class);
        collectionPlaylistEntity.setGmtCreate(new Date());
        collectionPlaylistEntity.setUserId(userId);
        return collectionPlaylistMapper.addCollectionPlaylist(collectionPlaylistEntity);
    }

    @Override
    public Integer removeByUserId(Long userId, Long playlistId) throws Exception {
        return collectionPlaylistMapper.removeByUserId(userId, playlistId);
    }
}
